package controllers

import (
	"CRMSystem/models"
	"CRMSystem/utils"
	"fmt"
)

type UserController struct {
	MainController
}

// Login 登录
func (c *UserController) Login() {
	username := c.GetString("username")
	password := utils.SHA256(c.GetString("password"))
	user := models.Users{
		UserName: username,
		Password: password,
	}
	boo := models.SelectByUsernamePassword(user)
	if boo {
		c.StartSession()
		id := getContactId(username)
		contact, err := models.SelectByContactId(id)
		err = c.SetSession("UserName", username)
		err = c.SetSession("Contact", contact)
		if err != nil {
			fmt.Println("error")
			return
		}
		c.Redirect("/page/main", 302)
	} else {
		c.Ctx.WriteString("登录失败")
	}
}

func getContactId(username string) int {
	return models.SelectContactIDByUserName(username)
}
